﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace FileBrower
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PopulateTreeView(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            // 添加菜单项并关联Click事件处理程序
            ToolStripMenuItem fileMenuItem = new ToolStripMenuItem("文件");
            fileMenuItem.DropDownItems.Add("打开", null, FileOpen_Click);
            fileMenuItem.DropDownItems.Add("保存", null, FileSave_Click);
            menuStrip1.Items.Add(fileMenuItem);

            // 添加工具栏按钮并关联Click事件处理程序
            ToolStripButton openButton = new ToolStripButton("打开", null, FileOpen_Click);
            toolStrip1.Items.Add(openButton);
            ToolStripButton saveButton = new ToolStripButton("保存", null, FileSave_Click);
            toolStrip1.Items.Add(saveButton);
        }
        private void FileOpen_Click(object sender, EventArgs e)
        {
            // 打开文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 在文本框中显示文件内容
                textBox1.Text = File.ReadAllText(openFileDialog.FileName);
            }
        }

        private void FileSave_Click(object sender, EventArgs e)
        {
            // 保存文件
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "文本文件 (*.txt)|*.txt";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 将文本框中的内容保存到文件
                File.WriteAllText(saveFileDialog.FileName, textBox1.Text);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void PopulateTreeView(string path)
        {
            //实现树形节点
            TreeNode rootNode = new TreeNode(path);
            string[] subDirs = Directory.GetDirectories(path);
            foreach (string subDir in subDirs)
            {
                TreeNode childNode = new TreeNode(subDir);
                rootNode.Nodes.Add(childNode);
            }
            treeView1.Nodes.Add(rootNode);
        }

        private void PopulateListView(string path)
        {
            //显示子文件
            listView1.Items.Clear();
            string[] subDirs = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);
            foreach (string subDir in subDirs)
            {
                ListViewItem item = new ListViewItem(subDir, 0);
                listView1.Items.Add(item);
            }
            foreach (string file in files)
            {
                ListViewItem item = new ListViewItem(file, 1);
                listView1.Items.Add(item);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //显示左侧文件节点的子文件
            TreeNode selectedNode = e.Node;
            PopulateListView(selectedNode.Text);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //对于右侧的txt文件，可以执行打开操作
            ListViewItem selectedItem = listView1.SelectedItems[0];
            string path = selectedItem.Text;
            if (File.Exists(path) && Path.GetExtension(path) == ".txt")
            {
                System.Diagnostics.Process.Start(path);
            }
        }
    }
}
